<template>
  <div class="item">
    <TipsItem :required="props.required" :label="props.label"></TipsItem>
    <div class="file">
      <el-upload class="custom-upload" list-type="picture-card" action ref="upload" :limit="1">
        
        <!-- 样式部分 -->
        <div class="upload">
          <div class="btn">
            <!-- <img src="@/assets/images/icon_plus.png" alt="" /> -->
            <el-icon :size="'26px'"><Plus /></el-icon>
            <span>点击上传</span>
          </div>
        </div>
        <template #file="{ file }">
          <div>
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleDownload(file)"
              >
                <el-icon><Download /></el-icon>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <el-icon><Delete /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-upload>
      <div class="text">
        请上传照片，最多上传一张，支持png，jpg，jpeg格式，不超过2M
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
import type { UploadFile } from 'element-plus'
const props = defineProps({
  required: {
    type: Boolean,
    default: false,
  },
  label: {
    type: String,
    default: "",
  },
  placeholder: {
    type: String,
    default: "",
  },
  inputValue: {
    type: [String, Number],
    default: "",
  },
});
const emits = defineEmits(["changeText"]);

//监听文本
const InputValue = ref(props.inputValue);
watch(
  () => {
    props.inputValue;
  },
  (newValue) => {
    InputValue.value = newValue;
  }
);
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)
const handleRemove = (file: UploadFile) => {
  console.log(file)
}

const handlePictureCardPreview = (file: UploadFile) => {
  console.log(file)
  dialogImageUrl.value = file.url!
  dialogVisible.value = true
}
//文件下载
const handleDownload = (file: UploadFile) => {
  console.log(file.url);
  // if(file.url!=null && file.url!=""){
  //   window.location.href = file.url;
  // }
  
  fetch(file.url)
  .then(response => response.blob())
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'image.jpg';
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
    document.body.removeChild(a);
  })
  .catch(error => console.error('下载图片失败', error));
}
// const changeText = () => {
//   // 自定义事件
//   emits("changeText", InputValue.value);
// };
</script>

<style lang="scss" scoped>
@import "@/assets/styles/theme.scss";
.title {
  font-size: 18px;
  font-weight: 700;
  display: flex;
  margin-bottom: 10px;
  margin-top: 20px;
  .required {
    color: red;
    margin-right: 5px;
  }
}
.file {
  color: $grey;
  margin-top: 10px;
  .upload {
    width: 120px;
    height: 120px;
    background-color: #f5f6f9;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 3px;

    .btn {
      width: 100px;
      height: 100px;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: $green;
      transition: all 0.3 ease-in-out;
      border: 1px dashed #c8cfd0;
      border-spacing: 30px;
      border-width: 1px;
      border-radius: 3px;
      > img {
        width: 18px;
        height: 18px;
        margin-bottom: 30px;
      }
      >span{
        margin-top: 10px;
      }
    }
    .btn:hover {
      transform: scale(1.03);
    }
  }

  .text {
    margin: 20px 0;
  }
}

::v-deep .el-upload{
  //去除照片墙 边框
  border:0;
  
}
::v-deep .el-upload--picture-card {
  --el-upload-picture-card-size:120px;
  background-color: inherit; /* 取消 hover 背景色 */
  border-color: inherit; /* 取消 hover 边框色 */
  box-shadow: none; /* 取消 hover 阴影 */
  cursor: auto; /* 取消 hover 光标样式 */
  margin: 0 0px;
  
}
//上传列表
::v-deep .el-upload-list__item-actions:hover {
  color: $green;
  background-color: inherit; /* 取消 hover 背景色 */
  border-color: inherit; /* 取消 hover 边框色 */
  box-shadow: none; /* 取消 hover 阴影 */
  cursor: auto; /* 取消 hover 光标样式 */
}
//上传列表
::v-deep .el-upload-list--picture-card{
  --el-upload-list-picture-card-size:120px;
}

</style>
